package com.sanpu.kxe.business.main.presenter;

import android.text.TextUtils;

import com.sanpu.kxe.business.main.bean.CertificationOldOutput;
import com.sanpu.kxe.business.main.bean.CertificationOutput;
import com.sanpu.kxe.business.main.bean.LinkfaceCheckOutput;
import com.sanpu.kxe.business.main.contract.LinkfaceCheckContract;
import com.sanpu.kxe.business.main.model.MainModel;
import com.sanpu.kxe.business.main.provider.MainOpenProvider;
import com.sanpu.kxe.utils.ToastUtil;
import com.sanpu.kxe.utils.VerifyUtil;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：认证数据校验
 * Created by 薛勇军 on 2019/5/22.
 */

public class LinkfaceCheckPresenter implements LinkfaceCheckContract.Presenter {

    private LinkfaceCheckContract.View mView;

    private CompositeSubscription mSubscription;

    private CertificationOutput mCertificationOutput;

    public LinkfaceCheckPresenter(LinkfaceCheckContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void initDataView(CertificationOutput certificationOutput) {
        this.mCertificationOutput = certificationOutput;
        if (certificationOutput == null) {
            mCertificationOutput = new CertificationOutput();
            mView.showLoadingDialog(false);
            Subscription subscription = MainModel.getInstance().getAuthInfo()
                    .subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<CertificationOldOutput>() {
                        @Override
                        public void onCompleted() {
                            mView.dismissLoadingDialog();
                        }

                        @Override
                        public void onError(Throwable e) {
                            mView.dismissLoadingDialog();
                            mView.showErrorText(e);
                        }

                        @Override
                        public void onNext(CertificationOldOutput authInfoOutput) {
                            mView.showViewData(authInfoOutput);
                        }
                    });
            mSubscription.add(subscription);
        }
    }

    @Override
    public void onCommitData(String name, String identityNumber, String bankCardNumber, String bankPhoneNumber) {
        if (TextUtils.isEmpty(name)) {
            ToastUtil.showTextViewPrompt("必须填写名字");
            return;
        }
        if (TextUtils.isEmpty(identityNumber)) {
            ToastUtil.showTextViewPrompt("必须填写身份证号");
            return;
        }
        if (VerifyUtil.checkIdValidation(identityNumber)) {
            ToastUtil.showTextViewPrompt("身份证号填写不正确");
            return;
        }
        if (TextUtils.isEmpty(bankCardNumber)) {
            ToastUtil.showTextViewPrompt("结算卡号不能为空");
            return;
        }
        if (TextUtils.isEmpty(bankPhoneNumber)) {
            ToastUtil.showTextViewPrompt("手机号不能为空");
            return;
        }
        if (VerifyUtil.checkPhoneNumber(bankPhoneNumber)) {
            ToastUtil.showTextViewPrompt("请输入正确的手机号");
            return;
        }
        mCertificationOutput.setCardNo(bankCardNumber);
        mCertificationOutput.setPhoneNo(bankPhoneNumber);
        mView.showLoadingDialog(false, "正在认证中...");
        Subscription subscription = MainModel.getInstance()
                .auth(name, identityNumber, bankCardNumber, bankPhoneNumber)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LinkfaceCheckOutput>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(LinkfaceCheckOutput checkOutput) {
                        if (checkOutput != null) {
                            mCertificationOutput.setCardType(checkOutput.getCardType());
                            mCertificationOutput.setBankNo(checkOutput.getBankNo());
                            mCertificationOutput.setBankName(checkOutput.getBankName());
                            mCertificationOutput.setBankAbbreviation(checkOutput.getBankAbbreviation());
                            MainOpenProvider.getInstance()
                                    .openLinkfaceLivenessActivity(mView.getContext(), mCertificationOutput);
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
